<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * @package    symfony
 * @subpackage plugin
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfGuardUserPeer.php 9999 2008-06-29 21:24:44Z fabien $
 */
class sfGuardUserPeer extends PluginsfGuardUserPeer
{
	
	public static function doSelectRelatedByPartner(Criteria $criteria, PropelPDO $con = null)
	{
		if(sfContext::getInstance()->getUser()->hasCredential('partner') and !sfContext::getInstance()->getUser()->isSuperAdmin())
		{
			$criteria->add(sfGuardUserPeer::PARTNER_ID,sfContext::getInstance()->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
			$rez = sfGuardUserPeer::doSelect($criteria);
		}
		return sfGuardUserPeer::populateObjects(sfGuardUserPeer::doSelectStmt($criteria, $con));
	}
	

	
	
	
public static function doCountRelatedByPartner(Criteria $criteria, $distinct = false, PropelPDO $con = null)
	{
				$criteria = clone $criteria;
		if(sfContext::getInstance()->getUser()->hasCredential('partner') and !sfContext::getInstance()->getUser()->isSuperAdmin())
		{
			$criteria->add(sfGuardUserPeer::PARTNER_ID,sfContext::getInstance()->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
			$rez = sfGuardUserPeer::doSelect($criteria);
		}
								$criteria->setPrimaryTableName(sfGuardUserPeer::TABLE_NAME);

		if ($distinct && !in_array(Criteria::DISTINCT, $criteria->getSelectModifiers())) {
			$criteria->setDistinct();
		}

		if (!$criteria->hasSelectClause()) {
			sfGuardUserPeer::addSelectColumns($criteria);
		}

		$criteria->clearOrderByColumns(); 		$criteria->setDbName(self::DATABASE_NAME); 
		if ($con === null) {
			$con = Propel::getConnection(sfGuardUserPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}


    foreach (sfMixer::getCallables('BasesfGuardUserPeer:doCount:doCount') as $callable)
    {
      call_user_func($callable, 'BasesfGuardUserPeer', $criteria, $con);
    }


				$stmt = BasePeer::doCount($criteria, $con);

		if ($row = $stmt->fetch(PDO::FETCH_NUM)) {
			$count = (int) $row[0];
		} else {
			$count = 0; 		}
		$stmt->closeCursor();
		return $count;
	}
	
	
	
	
	public static function getTop10($gender='w')
	{

        $slot = new CacheSlotTop10();
            if (false === ($data = $slot->load()))
            {
                $c = new Criteria();
                $c->add(sfGuardUserPeer::IS_ACTIVE,true);
                $c->add(sfGuardUserPeer::IS_OTPUSK,false);
                $c->add(sfGuardUserPeer::IS_TOP,true);
                $c->add(sfGuardUserPeer::GENDER,$gender);
                $c->addAscendingOrderByColumn('rand()');
                $c->setLimit(3);
                $data = sfGuardUserPeer::doSelect($c);
                $slot->addTag(new CacheTagTop10());
                $slot->save($data);
            }
             return $data;
	}
	
	
	public static function getTop5($gender='w',$offset)
	{
		
		$c = new Criteria();
        $c->add(sfGuardUserPeer::IS_ACTIVE,true);  
        $c->add(sfGuardUserPeer::IS_OTPUSK,false);
        $c->add(sfGuardUserPeer::IS_TOP,true);
        
        if($offset==5)
        {
        	$c->setOffset(4);
        }
        $c->setLimit(5);
        $c->add(sfGuardUserPeer::GENDER,$gender);
        $users = sfGuardUserPeer::doSelect($c);
        return $users;
	}
	
	
	public static function getRandomAvatars($count=5,$id)
	{
         $slot = new CacheSlotRandAvatars($count.$id);
         if (false === ($data = $slot->load()))
            {
            $c = new Criteria();
            $c->add(sfGuardUserPeer::IS_ACTIVE,true);
            $c->add(sfGuardUserPeer::IS_OTPUSK,false);
    //        $c->add(sfGuardUserPeer::IS_TOP,true);
            $c->add(sfGuardUserPeer::GENDER,'w');
            $c->add(sfGuardUserPeer::PHOTO,'',Criteria::NOT_EQUAL);
            $c->addAscendingOrderByColumn('rand()');
            //$c->add(sfGuardUserPeer::IS_TOP,true);
            $c->setLimit($count);
            $av = sfGuardUserPeer::doSelect($c);
            $data = $av;
            $slot->addTag(new CacheTagRandAvatars($count));
            $slot->save($data);
            }
            return $data;
	}

       public static function getAllPartners() {

		$c = new Criteria ( );
                $c->addDescendingOrderByColumn(sfGuardUserPeer::ID);
		$c->add ( sfGuardUserPeer::IS_PARTNER, true);
		$cnt = sfGuardUserPeer::doSelect ( $c );
		return $cnt;
	}

       public static function getAllPartnersArray() {

		$c = new Criteria ( );
                $c->addDescendingOrderByColumn(sfGuardUserPeer::ID);
		$c->add ( sfGuardUserPeer::IS_PARTNER, true);
		$cnt = sfGuardUserPeer::doSelect ( $c );

                $arr = array();
                foreach($cnt as $c)
                    {
                        $arr[$c->getId()]=$c;
                    }
		return $arr;
	}


       public static function getUsersForGender($gender='m') {

		$c = new Criteria ( );
                $c->add(sfGuardUserPeer::GENDER,$gender);
		$c->add ( sfGuardUserPeer::IS_ACTIVE, true);
                $c->add ( sfGuardUserPeer::IS_PARTNER, false);
                $c->add ( sfGuardUserPeer::IS_SUPER_ADMIN, false);
		$cnt = sfGuardUserPeer::doSelect ( $c );
		return $cnt;
	}

       public static function getUserVideos($user_id) {

		$c = new Criteria ( );
                $c->add(VideoPeer::USER_ID,$user_id);
		$cnt = VideoPeer::doSelect ( $c );
		return $cnt;
	}
	
       public static function getUserPhoto($user_id) {

		$c = new Criteria ( );
                $c->add(GalleryPeer::USER_ID,$user_id);
		$cnt = GalleryPeer::doSelect ( $c );
		return $cnt;
	}

              public static function retrieveByPK($pk, PropelPDO $con = null)
	{

            $slot = new CacheSlotUserById($pk);
            if (false === ($data = $slot->load()))
            {
              $data = parent::retrieveByPk($pk, $con);
              $slot->addTag(new CacheTagUserById($pk));
              $slot->save($data);
            }
            return $data;

	}
	
}
